package com.example.springboot.guava;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * </p>
 * http://ifeve.com/google-guava-collectionutilities/
 * @author Zero
 * @since 2019/6/15
 */
public class Test {

    public static void main(String[] args) {
        test0();
        test1();
        test2();
        test3();
    }

    /**
     * 示例
     */
    private static void test0() {
        List<String> list = Lists.newArrayList("alpha", "beta", "gamma");
        Set<Integer> set = Sets.newHashSet(1, 2, 3);
        Map<String, Integer> map = Maps.newHashMap();
        map.put("alpha", 1);
        map.put("beta", 2);
        map.put("gamma", 3);
        System.out.println("list: " + list);
        System.out.println("set: " + set);
        System.out.println("map: " + map);
    }

    /**
     * 交集、并集、差集
     */
    private static void test1() {
        Set<Integer> set1 = Sets.newHashSet(1, 2, 3, 4, 5, 6);
        Set<Integer> set2 = Sets.newHashSet(5, 6, 7, 8, 9, 10);

        System.out.println("交集：");
        Sets.SetView<Integer> intersection = Sets.intersection(set1, set2);
        for (Integer temp : intersection) {
            System.out.println(temp);
        }

        System.out.println("并集：");
        Sets.SetView<Integer> union = Sets.union(set1, set2);
        for (Integer temp : union) {
            System.out.println(temp);
        }

        System.out.println("差集：");
        Sets.SetView<Integer> difference = Sets.difference(set1, set2);
        for (Integer temp : difference) {
            System.out.println(temp);
        }
    }

    /**
     * 把List按指定大小分割
     */
    private static void test2() {
        List<String> list = Lists.newArrayList();
        for (int i = 0; i < 100; i++) {
            list.add("key" + i);
        }
        List<List<String>> lists = Lists.partition(list, 10);
        for (int i = 0; i < lists.size(); i++) {
            List<String> rList = lists.get(i);
            System.out.println("partition: " + rList);
        }
    }

    /**
     * 返回给定List的反转视图
     */
    private static void test3() {
        List<String> list = Lists.newArrayList();
        for (int i = 0; i < 10; i++) {
            list.add("key" + i);
        }
        List<String> newList = Lists.reverse(list);
        System.out.println("reverse before: " + list);
        System.out.println("reverse after: " + newList);
    }

}
